package com.ciwong.epaper.modules.pad.homeworkwidget.widget;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.util.Log;

import com.ciwong.epaper.modules.pad.homeworkwidget.common.AppWidgetConstants;
import com.ciwong.epaper.modules.pad.homeworkwidget.common.util.Serviceutil;
import com.ciwong.epaper.modules.pad.homeworkwidget.presenter.DataProvider;
import com.ciwong.epaper.modules.pad.homeworkwidget.presenter.HomeworkWidgetCheckState;
import com.ciwong.epaper.modules.pad.homeworkwidget.presenter.PersonalInfor;
import com.ciwong.epaper.modules.pad.homeworkwidget.widget.remoteviews.HomeworkRemoteViewsDecorator;
import com.ciwong.libs.utils.CWLog;


public class HomeworkReceiver extends BroadcastReceiver {


    private final  String TAG = "HomeworkReceiver";//延时时间
//    private final long DELAY_TIME_INTERACTIVE_TUTORIAL = 2000;//延时时间
//    private final Handler handler = new Handler();

    @Override
    public void onReceive(final Context context, final Intent intent) {
        Log.e("HomeworkReceiver", "lzh 接到广播： HomeworkReceiver  " + intent.getAction());
        if ("android.intent.action.SCREEN_ON".equals(intent.getAction())) {//接收到亮屏广播
            makeServiceActive(context);
            refreshFromScreenOn(context, intent);
        } else if ("android.intent.action.BOOT_COMPLETED".equals(intent.getAction())) {//接收到开机广播
            makeServiceActive(context);
            refreshFromScreenOn(context, intent);
        } else if ("android.net.conn.CONNECTIVITY_CHANGE".equals(intent.getAction())) {//接收到网络切换广播
//            refreshFromScreenOn(context, intent);
//            System.out.println("lzh 接到广播： HomeworkReceiver  " + intent.getAction());
        }/*else if ("android.intent.action.SCREEN_OFF".equals(intent.getAction())) {//接收到息屏广播
            System.out.println("lzh 接到广播： HomeworkReceiver  " + intent.getAction());
        }else if ("android.intent.action.USER_PRESENT".equals(intent.getAction())) {//接收到解锁广播
            System.out.println("lzh 接到广播： HomeworkReceiver  " + intent.getAction());
        }*/

       /* //接收到 登陆或者加入班级后刷新UI
        else if (AppWidgetConstants.ACTION_HAS_LOGIN_SUCESS.equals(intent.getAction())) {//接收到登陆成功widget
            ReceverPresenter.getInstance().widgetLoginSuccess(context, intent);
        }else if (LoginToken.ACTION_LOGIN_SUCCESS.equals(intent.getAction())) {//接收到登陆成功(app内部)
            ReceverPresenter.getInstance().appLoginSuccess(context, intent);
        } else if (LoginToken.ACTION_BIND_PHONE_SUCCESS.equals(intent.getAction())) {//接收绑定数据成功（app内部）
            ReceverPresenter.getInstance().bindPhoneSuccess(context, intent);
        } else if (LoginToken.ACTION_EXIT_LOGIN.equals(intent.getAction())) {//接收到退出登录
            ReceverPresenter.getInstance(). logoutSuccess(context, intent);
        } else if (EpaperConstant.BROADCAST_CLASS_CHANGE.equals(intent.getAction())) {//接收到加入班级成功（操作加入班级时的广播）
            ReceverPresenter.getInstance(). joinInClassSuccess(context, intent);
        } else if (AppWidgetConstants.ACTION_HAS_JOINED_CLASS.equals(intent.getAction())) {//网络检测到已经加入班级
            ReceverPresenter.getInstance(). hasJoinedInClassSuccess(context, intent);
        }


        // 接收刷新作业列表消息.去请求网络数据
        else if (EpaperConstant.BROADCAST_JPUSH_GET_CUSTOM_MSG.equals(intent.getAction())) {//收到极光消息
            ReceverPresenter.getInstance().getNetHomeworklist(context);
        } else if (EpaperConstant.BROADCAST_JPUSH_GET_NOTICE_MSG.equals(intent.getAction())) {//收到极光消息
            ReceverPresenter.getInstance().getNetHomeworklist(context);
        } else if (EpaperConstant.BROADCAST_HOME_WORK_STATUS_CHANGE_MSG.equals(intent.getAction())) {//作业状态改变消息
            handler.postDelayed(new Runnable() {
                @Override
                public void run() {
                    ReceverPresenter.getInstance().getNetHomeworklist(context);
                }
            }, DELAY_TIME_INTERACTIVE_TUTORIAL);

        } else if (EpaperConstant.BROADCAST_HOME_WORK_STATUS_INTERACTIVE_TUTORIAL_FINASH.equals(intent.getAction())) {//作业状态改变消息
            //专门为互动教程 延时请求处理
            handler.postDelayed(new Runnable() {
                @Override
                public void run() {
                    ReceverPresenter.getInstance().getNetHomeworklist(context);
                }
            }, DELAY_TIME_INTERACTIVE_TUTORIAL);
        }

        //请求数据后（网络或者本地数据）刷新UI
        else if (AppWidgetConstants.ACTION_LOAD_LOCAL_HOMEWORK_SUCCESS.equals(intent.getAction())) {//获取本地作业成功
            ReceverPresenter.getInstance(). refreshListData(context, intent);
        } else if (AppWidgetConstants.ACTION_LOAD_NET_HOMEWORK_SUCCESS.equals(intent.getAction())) {//获取网络作业成功
            ReceverPresenter.getInstance().refreshListData(context, intent);
        } else if (AppWidgetConstants.ACTION_LOAD_NET_HOMEWORK_FAILED.equals(intent.getAction())) {//获取网络作业失败
            ReceverPresenter.getInstance().refreshListData(context, intent);
        } else {
            System.out.println("lzh 接到未知广播： HomeworkReceiver  " + intent.getAction());
        }*/
    }

    /**
     * 确保服务运行
     *
     * @param context
     */
    private void makeServiceActive(Context context) {
        if (!Serviceutil.isServiceRunning(context, AppWidgetConstants.serviceName)) {
            context.startService(new Intent(context, HomeworkwidgetmService.class));
        } else {
            CWLog.e(TAG, "lzh HomeworkWidgetProvider isServiceRunning 运行中 ");
        }
    }

    /**
     * 接收到亮屏广播刷新
     *
     * @param context
     * @param intent
     */
    private void refreshFromScreenOn(Context context, Intent intent) {
        getNetHomeworklist(context);
        //接收亮屏刷新个人信息
        PersonalInfor.getInstance(context).refreshPerInfor(HomeworkWidgetCheckState.isLogin(context), HomeworkWidgetCheckState.isBindPhone());
        refreshView(context, intent);
    }
    /**
     * 加载网络数据
     *
     * @param context
     */
    private void getNetHomeworklist(Context context) {
        DataProvider.getMyWorkList(context);
    }
    /**
     * 刷新整个界面
     *
     * @param context
     * @param intent
     */
    private void refreshView(Context context, Intent intent) {
        HomeworkRemoteViewsDecorator.getInstance().checkIsLoginAndJoinClassState(context);
    }
}
